import jieba
import os


class StringTools(object):
    # 取字符串中两个符号之间的东东
    @staticmethod
    def txt_wrap_by(start, end, origin):
        position = origin.find(start)
        if position >= 0:
            position += len(start)
            end = origin.find(end, position)
            if end >= 0:
                return origin[position:end].strip()

    @staticmethod
    def skip(index_start, string):
        symbol = [":", "：", "|", " "]
        for char in symbol:
            if char == string[index_start:index_start + 1]:
                return StringTools.skip(index_start + 1, string)
        else:
            return index_start

    # 跳过一些词
    @staticmethod
    def find_excluding(raw, keyword):
        return StringTools.__find_excluding(raw, keyword, 0)

    @staticmethod
    def __find_excluding(raw, keyword, index):
        excluding = ["详见"]
        for string in excluding:
            index = raw.find(keyword, index)
            if index > 0:
                index_skip = StringTools.skip(index + len(keyword), raw)
                if string == raw[index_skip:(index_skip + 2)]:
                    return StringTools.__find_excluding(raw, keyword, index_skip)
                else:
                    return index
        else:
            return 0

    # 定义结束位子
    @staticmethod
    def end(index_start, string):
        symbol = ["\n", ":", "：", "|", " ", "(", ")", "（", "）"]
        index = index_start
        flag = 0
        for char in string[index_start:]:
            for end_char in symbol:
                if end_char == char:
                    return index + flag
            flag = flag + 1
        return index_start

    # 去除标记
    @staticmethod
    def remove_symbol(raw):
        symbol = ["\n", " ", "？"]
        return StringTools.__remove_symbol(raw, symbol)

    @staticmethod
    def removeAll_symbol(raw):
        symbol = ["\n", ":", "：", " ", "-", "(", ")", "？"]
        return StringTools.__remove_symbol(raw, symbol)

    @staticmethod
    def __remove_symbol(raw, symbol):
        for char in symbol:
            raw = raw.replace(char, "")
        return raw

    # 分词，并除去部分词汇
    @staticmethod
    def jieba(raw, pass_word):
        path = os.path.split(os.path.realpath(__file__))[0]
        jieba.load_userdict(path + "/dict.txt")
        seg_list = jieba.lcut_for_search(raw)  # 分词
        seg_list = list(set(seg_list))
        for ex in pass_word:
            try:
                # 去除！以防重复性太高
                seg_list.remove(ex)
            except:
                a = 1
        for seg in seg_list:
            if len(seg) < 2:
                seg_list.remove(seg)
        return seg_list
    @staticmethod
    def institute_type(tender_title):
        institute_type = '政府机构'
        if '医院' in tender_title or '妇幼保健院'in tender_title or \
                        '防治院' in tender_title or '防治所' in tender_title or '防治站' in tender_title:
            institute_type = '医院'
        elif '卫生服务中心' in tender_title or '卫生服务站'in tender_title or \
                        '卫生院' in tender_title or '疗养院' in tender_title:
            institute_type = '基层医疗单位'
        elif '门诊部' in tender_title or '诊所'in tender_title or \
                        '卫生所' in tender_title or '医务室' in tender_title or \
                        '保健所' in tender_title or '卫生站' in tender_title:
            institute_type = '门诊/诊所'
        elif '临床检验中心' in tender_title:
            institute_type = '临床检验中心'
        elif '急救中心' in tender_title or '急救站'in tender_title or \
                        '护理院' in tender_title or '护理站' in tender_title or \
                        '血站' in tender_title or '疾控中心' in tender_title:
            institute_type = '其他诊疗机构'
        elif '公司' in tender_title:
            institute_type = '企业'
        elif '大学' in tender_title:
            institute_type = '大学'
        return institute_type